<?php
/**
 * Created by PhpStorm.
 * User: linzh
 * Date: 2017-05-10
 * Time: 11:49
 */

declare(strict_types=1);

namespace {

    use application\module\admin\addon\Sign;
    use wshore\core\Session;
    use wshore\helper\Chars;

    if (filter_var($_SERVER['HTTP_HOST'], FILTER_VALIDATE_IP)) {
        # 拒绝IP访问
        die('ip access deny');
    }
    define('IS_ONLINE', (strpos($_SERVER['HTTP_HOST']??'', 'pgyxwd.com') !== false));

    require __DIR__ . '/../../wshore/engine.inc';

    wshore::initialize([
        'lite.database.dao' => [
            WS_ADAPTER_CLASS => ['wshore\\core\\database\\dao\\MySQL',],
            WS_ADAPTER_CONFIG => [
                [
                    WS_DAO_DBNAME => 'ischema',//选择的数据库
                    WS_DAO_USERNAME => 'lin',
                    WS_DAO_PASSWORD => '123456',
                    WS_DAO_HOST => '127.0.0.1',
                    WS_DAO_PORT => 3306,
                    WS_DAO_CHARSET => 'UTF8',
                    WS_DAO_DSN => null,//默认先检查差DSN是否正确,直接写dsn而不设置其他的参数可以提高效率，也可以避免潜在的bug
                    WS_DAO_OPTIONS => [
                        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,//默认异常模式
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,//结果集返回形式
                    ],
                ],
            ],
        ],
    ]);

    # 检测是否授权访问
    if (!Session::get('_auth_explorer_', false)) {
        die('no permission');
    }

    $sign = Sign::getInstance();
    try {
        $sign->getUser();
        $_SESSION['kod_login'] = true;
        $_SESSION['kod_user'] = 'demo';
        $_SESSION['CSRF-TOKEN'] = Chars::random(20);
        $expire = time() + 3600 * 24 * 100;
        setcookie('CSRF-TOKEN', $_SESSION['CSRF-TOKEN'], time() + 3600 * 24 * 100);
        setcookie('kod_user_id', '1', $expire);
        setcookie('kod_token', '1', $expire);

        require __DIR__ . '/../module/explorer/index.inc';
    } catch (\wshore\Exception $exception) {
        die('no permission');
    }
}
